package com.william.jdbcplus.core.condition.segment.impl;

import com.william.jdbcplus.core.enums.SqlKeyword;
import com.william.jdbcplus.core.toolkit.StringPool;
import lombok.Getter;
import lombok.Setter;

import java.util.stream.Collectors;

/**
 * Select SQL 片段
 * select 到 from 之间部分
 *
 * @author niliwei
 */
@Getter
@Setter
public class SelectSegmentList extends AbstractSegmentList {
    /**
     * 是否处理了的上个 not
     */
    private boolean isDistinct = false;

    @Override
    public boolean add(String segment) {
        if (this.contains(segment)) {
            return true;
        }

        return super.add(segment);
    }

    @Override
    public String getSegment() {
        if (isEmpty()) {
            return StringPool.EMPTY;
        }
        return this.stream().collect(Collectors.joining(StringPool.COMMA));
    }

    @Override
    public String getSql() {
        if (isEmpty()) {
            return StringPool.EMPTY;
        }

        String distinct = "";
        if (isDistinct()) {
            distinct = StringPool.SPACE + SqlKeyword.DISTINCT.toString();
        }
        return SqlKeyword.SELECT.getSegment() + distinct + StringPool.SPACE + getSegment();
    }
}
